package com.industrial.premu.service.impl.sqsj;



import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.DgQuantityQualityManage;
import com.industrial.premu.domain.PreMuCompanyInfo;
import com.industrial.premu.domain.SysOperError;
import com.industrial.premu.domain.sqsj.PreMuImportBenefitEvaluationNo;
import com.industrial.premu.domain.sqsj.PreMuImportLog;
import com.industrial.premu.mapper.DgQuantityQualityManageMapper;
import com.industrial.premu.mapper.PreMuCompanyInfoMapper;
import com.industrial.premu.mapper.SysOperErrorMapper;
import com.industrial.premu.mapper.sqsj.PreMuImportLogMapper;
import com.industrial.premu.service.IPreMuImportLogService;
import com.industrial.premu.util.ValidationUtil;
import com.industrial.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.industrial.premu.mapper.sqsj.PreMuImportTongjiMapper;
import com.industrial.premu.domain.sqsj.PreMuImportTongji;
import com.industrial.premu.service.IPreMuImportTongjiService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ValidationException;

/**
 * 亩产导入数据-统计局Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-25
 */
@Service
public class PreMuImportTongjiServiceImpl implements IPreMuImportTongjiService
{
    @Autowired
    private PreMuImportTongjiMapper preMuImportTongjiMapper;

    private static final int BATCH_SIZE = 100;

    @Autowired
    private PreMuCompanyInfoMapper preMuCompanyInfoMapper;

    @Autowired
    private DgQuantityQualityManageMapper dgQuantityQualityManageMapper;

    @Autowired
    private SysOperErrorMapper sysOperErrorMapper;

    /**
     * 查询亩产导入数据-统计局
     *
     * @param id 亩产导入数据-统计局主键
     * @return 亩产导入数据-统计局
     */
    @Override
    public PreMuImportTongji selectPreMuImportTongjiById(String id)
    {
        return preMuImportTongjiMapper.selectPreMuImportTongjiById(id);
    }

    /**
     * 查询亩产导入数据-统计局列表
     *
     * @param preMuImportTongji 亩产导入数据-统计局
     * @return 亩产导入数据-统计局
     */
    @Override
    public List<PreMuImportTongji> selectPreMuImportTongjiList(PreMuImportTongji preMuImportTongji)
    {

        return preMuImportTongjiMapper.selectPreMuImportTongjiList(preMuImportTongji);
    }

    /**
     * 新增亩产导入数据-统计局
     *
     * @param preMuImportTongji 亩产导入数据-统计局
     * @return 结果
     */
    @Override
    public int insertPreMuImportTongji(PreMuImportTongji preMuImportTongji)
    {
        return preMuImportTongjiMapper.insertPreMuImportTongji(preMuImportTongji);
    }

    /**
     * 修改亩产导入数据-统计局
     *
     * @param preMuImportTongji 亩产导入数据-统计局
     * @return 结果
     */
    @Override
    public int updatePreMuImportTongji(PreMuImportTongji preMuImportTongji)
    {
        return preMuImportTongjiMapper.updatePreMuImportTongji(preMuImportTongji);
    }

    /**
     * 批量删除亩产导入数据-统计局
     *
     * @param ids 需要删除的亩产导入数据-统计局主键
     * @return 结果
     */
    @Override
    public int deletePreMuImportTongjiByIds(String[] ids)
    {
        return preMuImportTongjiMapper.deletePreMuImportTongjiByIds(ids);
    }

    /**
     * 删除亩产导入数据-统计局信息
     *
     * @param id 亩产导入数据-统计局主键
     * @return 结果
     */
    @Override
    public int deletePreMuImportTongjiById(String id)
    {
        return preMuImportTongjiMapper.deletePreMuImportTongjiById(id);
    }


    @Override
    public AjaxResult importTongJiExcelData(String year, String companyType, MultipartFile file) {
        try{
            ExcelUtil<PreMuImportTongji> util = new ExcelUtil<PreMuImportTongji>(PreMuImportTongji.class);
            List<PreMuImportTongji> preMuImportTongjis = util.importExcel(file.getInputStream());
            if (StringUtils.isNull(preMuImportTongjis) || preMuImportTongjis.size() == 0) {
                Map<String,String> resultMap = new HashMap<>();
                resultMap.put("allMsg","导入数据不能为空");
                return AjaxResult.error("导入失败",resultMap);
            }
            SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
            if(StringUtils.equals(companyType,"1")){
                //规上统计局前置检查，规下导入时没有企业类型字段，无须检查
                StringBuilder errorInfo = new StringBuilder("<h3><b>以下企业类型与您导入时所选企业类型不符:</b></h3>");
                int errorCount = 1;

                for(PreMuImportTongji t : preMuImportTongjis){
                    if(!StringUtils.equals(t.getCompanyType(), companyType)){
                        errorInfo.append("<font color='red'>").append(String.format("%d、",errorCount)).append(t.getCompanyName()).append("</font><br/>");
                        errorCount++;
                    }
                }
                if(errorCount != 1){
                    Map<String,String> resultMap = new HashMap<>();
                    resultMap.put("allMsg",errorInfo.toString());
                    return AjaxResult.error("导入失败",resultMap);
                }
            }

            int updateNum = 0;
            int successNum = 0;
            int failureNum = 0;
            //企业名称为空的错误数据量
            int companyNameBlankCount = 0;
            //值为空的错误量
            int valueErrorCount = 0;
            //值小于等于0的错误量
            int valueZeroCount = 0;
            StringBuilder successMsg = new StringBuilder();
            StringBuilder failureMsg = new StringBuilder();
            StringBuilder allMsg = new StringBuilder();

            List<String> usccList = new ArrayList<String>();
            List<PreMuImportTongji> realList = new ArrayList<PreMuImportTongji>();
            List<String> mcImportExistList = new ArrayList<String>();

            List<PreMuImportTongji> addImportList = new ArrayList<PreMuImportTongji>();
            List<PreMuImportTongji> updateImportList = new ArrayList<PreMuImportTongji>();
            List<PreMuCompanyInfo> updateCompanyInfoDOList = new ArrayList<PreMuCompanyInfo>();

            for(int i =0; i < preMuImportTongjis.size();i++){
                PreMuImportTongji item = preMuImportTongjis.get(i);
                if (item.getUscc() != null && !"".equals(item.getUscc())){
//                    if("1".equals(item.getCompanyType())){
//                        if(StringUtils.isBlank(item.getTotalUsed())) {
//                            return AjaxResult.warn("总能耗为空或异常，请检查导入数据！");
//                        }
//                        if(StringUtils.isBlank(item.getIndustryOutput())) {
//                            return AjaxResult.warn("工业总产值为空或异常，请检查导入数据！");
//                        }
//                    }else if("2".equals(companyType)){
//                        item.setCompanyType("2");
//                    }
                    if("2".equals(item.getCompanyType())){
                        item.setCompanyType("2");
                    }
                    usccList.add(item.getUscc().trim());
                    realList.add(item);
                }
            }
            long count = usccList.stream().distinct().count();
            boolean isRepeat = count < usccList.size();
            if (isRepeat) {
                Map<String,String> resultMap = new HashMap<>();
                resultMap.put("allMsg","有重复统一社会信用代码，请检查导入数据！");
                return AjaxResult.error("导入失败",resultMap);
            }
            mcImportExistList = preMuImportTongjiMapper.getImportTongjiExistList(year, usccList);

            ValidationUtil.validate(realList);

            int travel = 1;
            for (int i = 0; i < preMuImportTongjis.size(); i++) {
                travel++;
                PreMuImportTongji preMuImportTongji = preMuImportTongjis.get(i);
                preMuImportTongji.setYear(year);
                preMuImportTongji.setCompanyType(companyType);
                String uscc = preMuImportTongji.getUscc();
                if (uscc == null || "".equals(uscc) || year == null || "".equals(year)) {
                    continue;
                }else{
                    uscc = uscc.trim();
                    preMuImportTongji.setUscc(uscc);
                }
                if (preMuImportTongji.getCompanyProp() == null || "".equals(preMuImportTongji.getCompanyProp())) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
                    failureMsg.append(msg + "企业性质不能为空");
                    continue;
                }else {
                    if (!preMuImportTongji.getCompanyProp().endsWith("0")) {
                        preMuImportTongji.setCompanyProp(preMuImportTongji.getCompanyProp().substring(0, 2) + "0");
                    }
                }

                if(StringUtils.isBlank(preMuImportTongji.getCompanyName())){
                    failureNum++;
                    companyNameBlankCount++;
                    continue;
                }

                String numRegx = "\\d+";
                String numPointRegx = "^\\d+(\\.\\d+)?$";
                if (StringUtils.isNotEmpty(preMuImportTongji.getCompanyProp())
                        && !Pattern.matches(numRegx, preMuImportTongji.getCompanyProp())){
                    failureNum++;
                    valueErrorCount++;
                    String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
                    failureMsg.append(msg + "企业性质需要填对应代码");
                    continue;
                }

                //规上企业独有的
                if("1".equals(preMuImportTongji.getCompanyType())){
                    if(StringUtils.isBlank(preMuImportTongji.getTotalUsed()) || !Pattern.matches(numPointRegx, preMuImportTongji.getTotalUsed()) ||
                            StringUtils.isBlank(preMuImportTongji.getIndustryOutput()) || !Pattern.matches(numPointRegx, preMuImportTongji.getIndustryOutput()) ){
                        valueErrorCount++;
                        failureNum++;
                        String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
                        failureMsg.append(msg + "总能耗、工业总产值不能为空或不是正确数字");
                        continue;
                    }

                    BigDecimal totalUsedBig =  StringUtils.isBlank(preMuImportTongji.getTotalUsed()) ? BigDecimal.ZERO : new BigDecimal(preMuImportTongji.getTotalUsed());
                    BigDecimal industryOutputBig =  StringUtils.isBlank(preMuImportTongji.getIndustryOutput()) ? BigDecimal.ZERO : new BigDecimal(preMuImportTongji.getIndustryOutput());

                    //只有规上企业才需要判断总能耗或工业总产值是否为空
                    if(totalUsedBig.compareTo(BigDecimal.ZERO) < 1){
                        failureNum++;
                        String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
                        failureMsg.append(msg + "总能耗不能小于等于0");
                        valueZeroCount++;
                        continue;
                    }

                    if(StringUtils.isBlank(preMuImportTongji.getIndustryOutput()) ){
                        String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
                        failureMsg.append(msg + "工业总产值不能为空");
                        valueZeroCount++;
                        failureNum++;
                        continue;
                    }

                    preMuImportTongji.setTotalUsed(totalUsedBig.setScale(2, RoundingMode.HALF_UP).toString());

                    if (preMuImportTongji.getCompanyType() == null || "".equals(preMuImportTongji.getCompanyType())) {
                        failureNum++;
                        valueErrorCount++;
                        String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
                        failureMsg.append(msg + "企业类型不能为空");
                        continue;
                    }
                    if (StringUtils.isNotEmpty(preMuImportTongji.getCompanyType())
                            && !Pattern.matches(numRegx, preMuImportTongji.getCompanyType())) {
                        failureNum++;
                        valueErrorCount++;
                        String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
                        failureMsg.append(msg + "企业类型需要填对应代码");
                        continue;
                    }
                }


//                if (preMuImportTongji.getIndustryType() == null || "".equals(preMuImportTongji.getIndustryType())) {
//                    failureNum++;
//                    valueErrorCount++;
//                    String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
//                    failureMsg.append(msg + "所属行业不能为空");
//                    continue;
//                }
//                if ("1".equals(preMuImportTongji.getCompanyType())&&(preMuImportTongji.getIndustryMediumType() == null || "".equals(preMuImportTongji.getIndustryMediumType()))) {
//                    failureNum++;
//                    valueErrorCount++;
//                    String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
//                    failureMsg.append(msg + "所属行业中类不能为空");
//                    continue;
//                }
//                if (StringUtils.isNotEmpty(preMuImportTongji.getIndustryType())
//                        && !Pattern.matches(numRegx, preMuImportTongji.getIndustryType())) {
//                    failureNum++;
//                    valueErrorCount++;
//                    String msg = "<br/>" + failureNum + "、企业 " + preMuImportTongji.getCompanyName() + " 导入失败：";
//                    failureMsg.append(msg + "所属行业需要填对应代码");
//                    break;
//                }

                if (mcImportExistList.contains(uscc)) {
                    updateImportList.add(preMuImportTongji);
                } else {
                    preMuImportTongji.setId(UUID.randomUUID().toString().replace("-", ""));
                    preMuImportTongji.setDelFlag("0");
                    addImportList.add(preMuImportTongji);
                }

                PreMuCompanyInfo preMuCompanyInfo = new PreMuCompanyInfo();
                if(preMuCompanyInfo != null){
                    preMuCompanyInfo.setUscc(preMuImportTongji.getUscc());
                    preMuCompanyInfo.setCompanyName(preMuImportTongji.getCompanyName());
                    preMuCompanyInfo.setCompanyProp(preMuImportTongji.getCompanyProp());
                    preMuCompanyInfo.setCompanyType(preMuImportTongji.getCompanyType());
                    preMuCompanyInfo.setCompanyScale(preMuImportTongji.getCompanyScale());
                    //preMuCompanyInfo.setIndustryType(preMuImportTongji.getIndustryType());
                    //preMuCompanyInfo.setIndustryMediumType(preMuImportTongji.getIndustryMediumType());
                    preMuCompanyInfo.setDetailAddr(preMuImportTongji.getAddr());
                    preMuCompanyInfo.setDelFlag("0");
                    updateCompanyInfoDOList.add(preMuCompanyInfo);
                }

                if (addImportList != null && addImportList.size() >= BATCH_SIZE) {
                    int importNum = preMuImportTongjiMapper.batchSave(addImportList);
                    successNum += importNum;
                    addImportList.clear();
                }
                if (updateImportList != null && updateImportList.size() >= BATCH_SIZE) {
                    int importNum = preMuImportTongjiMapper.batchUpdateByUscc(year, updateImportList);
                    successNum += importNum;
                    updateNum += importNum;
                    updateImportList.clear();
                }

                if (updateCompanyInfoDOList != null && updateCompanyInfoDOList.size() >= BATCH_SIZE) {
                    preMuCompanyInfoMapper.batchUpdateByUscc(updateCompanyInfoDOList);
                    updateCompanyInfoDOList.clear();
                }
            }

            if (addImportList != null && !addImportList.isEmpty()) {
                int importNum = preMuImportTongjiMapper.batchSave(addImportList);
                successNum += importNum;
                addImportList.clear();
            }
            if (updateImportList != null && !updateImportList.isEmpty()) {
                int importNum = preMuImportTongjiMapper.batchUpdateByUscc(year, updateImportList);
                successNum += importNum;
                updateNum += importNum;
                updateImportList.clear();
            }

            if (updateCompanyInfoDOList != null && !updateCompanyInfoDOList.isEmpty()) {
                preMuCompanyInfoMapper.batchUpdateByUscc(updateCompanyInfoDOList);
                updateCompanyInfoDOList.clear();
            }

            if (failureNum > 0) {
                allMsg.insert(0, "<br/>很抱歉，错误如下：" + failureMsg.toString());
            }
            if (successNum > 0) {
                allMsg.insert(0, "恭喜您，共" + successNum + " 条数据导入成功！");
            }
//            if (failureNum > 0) {
//                return AjaxResult.error("数据导入失败！"+allMsg);
//            }

            //企业名称为空的数量
            if(companyNameBlankCount > 0){
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("统计局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("企业名称不能为空");
                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());

                    String failCount = dgQuantityQualityManage.getFailCount();
                    Integer oldFailCount = Integer.parseInt(failCount);

                    dgQuantityQualityManage.setFailCount(String.valueOf(companyNameBlankCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }else{
                    comapnNameBlankParams.setBaseId(IdUtil.simpleUUID());
                    comapnNameBlankParams.setBaseCreateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseCreateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setBaseUpdateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseUpdateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setFailCount(String.valueOf(companyNameBlankCount));
                    comapnNameBlankParams.setRuleType("1");
                    dgQuantityQualityManageMapper.insertDgQuantityQualityManage(comapnNameBlankParams);
                }
            }else{
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("统计局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("企业名称不能为空");
                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());
                    dgQuantityQualityManage.setFailCount(String.valueOf(companyNameBlankCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }
            }

            //值小于等于0的错误量
            if(valueZeroCount > 0){
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("统计局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("值小于0的错误");

                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());

                    String failCount = dgQuantityQualityManage.getFailCount();
                    Integer oldFailCount = Integer.parseInt(failCount);

                    dgQuantityQualityManage.setFailCount(String.valueOf(valueZeroCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }else{
                    comapnNameBlankParams.setBaseId(IdUtil.simpleUUID());
                    comapnNameBlankParams.setBaseCreateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseCreateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setBaseUpdateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseUpdateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setFailCount(String.valueOf(valueZeroCount));
                    comapnNameBlankParams.setRuleType("1");
                    dgQuantityQualityManageMapper.insertDgQuantityQualityManage(comapnNameBlankParams);
                }
            }else{
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("统计局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("值小于0的错误");
                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());
                    dgQuantityQualityManage.setFailCount(String.valueOf(valueZeroCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }
            }

            //值存在错误的情况
            if(valueErrorCount > 0){
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("统计局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("值不存在或者是格式有误");

                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());

                    String failCount = dgQuantityQualityManage.getFailCount();
                    Integer oldFailCount = Integer.parseInt(failCount);

                    dgQuantityQualityManage.setFailCount(String.valueOf(valueErrorCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }else{
                    comapnNameBlankParams.setBaseId(IdUtil.simpleUUID());
                    comapnNameBlankParams.setBaseCreateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseCreateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setBaseUpdateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseUpdateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setFailCount(String.valueOf(valueErrorCount));
                    comapnNameBlankParams.setRuleType("1");
                    dgQuantityQualityManageMapper.insertDgQuantityQualityManage(comapnNameBlankParams);
                }
            }else{
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("统计局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("值不存在或者是格式有误");
                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());
                    dgQuantityQualityManage.setFailCount(String.valueOf(valueErrorCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }
            }
            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("msg","导入成功");
            resultMap.put("sum",String.valueOf(successNum));
            resultMap.put("updateNum",String.valueOf(updateNum));
            resultMap.put("failureNum",String.valueOf(failureNum));
            resultMap.put("allMsg",allMsg.toString());
            return AjaxResult.success(resultMap);
        }catch (Exception e){
            String requestURI = "/industrial-premu/importExcelData";
            SysOperError sysOperError = new SysOperError();
            sysOperError.setBaseId(IdUtil.simpleUUID());
            sysOperError.setBaseCreateTime(new Date());
            sysOperError.setErrorMsg(e.getMessage());
            sysOperError.setUrlAddress(requestURI);
            sysOperError.setOperId(SecurityUtils.getUserId());
            sysOperErrorMapper.saveErrorInfo(sysOperError);
            e.printStackTrace();
            if(e instanceof ValidationException) {
                return AjaxResult.error(e.getMessage());
            }
            return AjaxResult.error("导入失败");
        }

    }
    @Autowired
    private PreMuImportLogMapper preMuImportLogMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult removeData(PreMuImportTongji params,String deptCode) {
        preMuImportTongjiMapper.removeData(params);

        PreMuImportLog removeParams = new PreMuImportLog();
        removeParams.setYear(params.getYear());
        removeParams.setCounty(SecurityUtils.getLoginUser().getSysUser().getDistrict());
        removeParams.setDeptCode(deptCode);
        preMuImportLogMapper.removeData(removeParams);
        return AjaxResult.success();
    }
}
